import {Buffer} from "buffer";
import {AxdrInteger} from "../asn1.axdr/AxdrInteger";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";
import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";


export class Scaler_Unit implements AxdrType{

    dataCode : Buffer | null = null;
    t1 : AxdrInteger | null = null;
    unit : AxdrEnum | null = null;

    constructor() {

    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    set_all(t1 : AxdrInteger, unit : AxdrEnum){
        this.t1 = t1;
        this.unit = unit;
    }

    decode(input: ReverseByteArrayInputStream): number {

        return 0;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.unit != null && this.t1 != null){
                codeLength = 0;
                codeLength += this.unit.encode(output);

                codeLength += this.t1.encode(output);
            }else{
                throw new Error("unit or t1 is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.unit != null && this.t1 != null) {
            return "sequence: {"+ "t1: " + this.t1 + ", unit: " + this.unit  + "}";
        } else {
            return "unit or t1 is null";
        }
    }


}
